<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>

    <meta charset="utf-8">

    <title>以计算机科学与技术专业为例的学生学习过程分析系统</title>
    <!--引入相关的js文件-->
    <link href="../backPage/ui/bootstrap-3.3.0/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="../backPage/ui/bootstrap-table-1.11.0/bootstrap-table.min.css" rel="stylesheet"/>
    <link href="../backPage/ui/waves-0.7.5/waves.css" rel="stylesheet"/>
    <link href="../backPage/ui/jquery-confirm/jquery-confirm.min.css" rel="stylesheet"/>
    <link href="../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>

    <script src="../backPage/ui/js/jquery-3.3.1.min.js"></script>
    <script src="../backPage/ui/waves-0.7.5/waves.js"></script>
    <script src="../backPage/ui/jquery-confirm/jquery-confirm.min.js"></script>
    <script src="../backPage/ui/bootstrap-3.3.0/js/bootstrap.min.js"></script>
    <script src="../backPage/ui/bootstrap-table-1.11.0/bootstrap-table.min.js"></script>
</head>

<body>
<div id="main">
    <div id="toolbar">
        <button class="btn btn-default" href="javascript:;" onclick="createAction()">新增课程信息</button>
        <button class="btn btn-default" href="javascript:;" onclick="updateAction()">编辑课程信息</button>
        <button class="btn btn-default" href="javascript:;" onclick="deleteAction()">删除课程信息</button>
        <button class="btn btn-default" href="javascript:;" onclick="uploadAction()">导入课程信息</button>
        <button class="btn btn-default" href="javascript:;"><a style="text-decoration:none; color:black;" id="uploadTeacherModel" href="/download/uploadModel/UploadCourseInfos.xls">下载导入课程信息模板</a></button>
        <br/>
    </div>
    <table id="table"></table>
</div>

<script>
    // 初始化input特效
    function initMaterialInput() {
        $('form input[type="text"]').each(function () {
            if ($(this).val() != '') {
                $(this).parent().find('label').addClass('active');
            }
        });
    }

    var $table = $('#table');


    $(function() {
        init();
        //新增课程信息
        $("#submitbutton").click(function() {
            var coursename = $("#coursename").val();
            var coursescore = $("#coursescore").val();
            var coursetime = $("#coursetime").val();
            var coursetype = $("#coursetype option:selected").val();
            var courseteacher = $("#courseTeacher option:selected").val();
            var coursestartweek = $("#coursestartweek").val();
            var courseenddweek = $("#courseendweek").val();
            var coursestate = $("#coursestate option:selected").val();

            if(coursename.length == 0){
                alert("请正确填写课程名称!");
                return;
            }

            if(coursestartweek.length == 0){
                alert("请填写起始周信息");
                return;
            }

            if(courseendweek.length == 0){
                alert("请填写终止周信息");
                return;
            }



            coursescore = parseFloat(coursescore);
            coursetime = parseInt(coursetime);
            courseteacher = parseInt(courseteacher);
            coursestartweek = parseInt(coursestartweek);
            courseenddweek = parseInt(courseenddweek);

            if(coursestartweek <= 0){
                alert("起始周错误");
                return;
            }

            if(courseendweek <= 0){
                alert("终止周错误");
                return;
            }



            $.ajax({
                url:"/studentManageSystem/addCourseInfo",
                type:"post",
                data:{coursename:coursename,coursescore:coursescore,coursetime:coursetime,coursetype:coursetype,courseteacher:courseteacher,coursestartweek:coursestartweek,courseendweek:courseenddweek,coursestate:coursestate},
                success:function(data){
                    if(data=="OK"){
                        alert("添加成功");
                        $table.bootstrapTable('refresh');
                        $("#createModal").modal('hide');
                        $("#coursename").val("");
                        $("#coursetime").val("");
                        $("#coursescore").val("");
                    }else{
                        alert("添加失败");
                    }
                }
            })

        });


        //上传信息
        $("#uploadbutton").click(function(){
            var fname = $("#file").val();
            var pathArray = fname.split("\\");
            if(pathArray[pathArray.length - 1].indexOf(".") == -1){
                alert("请上传文件");
                return;
            }else{
                filetype = pathArray[pathArray.length - 1].split(".")[1];

                if(filetype == "xls"){
                    var type = "file";          //后台接收时需要的参数名称，自定义即可
                    var id = "file";            //即input的id，用来寻找值
                    var formData = new FormData();
                    formData.append(type, $("#"+id)[0].files[0]);    //生成一对表单属性
                    $.ajax({
                        type: "post",           //因为是传输文件，所以必须是post
                        url:"/studentManageSystem/uploadCourseInfos",
                        data: formData,
                        processData: false,
                        contentType: false,
                        success: function (data) {
                            if(data != ""){
                                $table.bootstrapTable('refresh');
                                alert(data);
                            }else{
                                alert("导入失败");
                            }
                        }
                    });

                }else{
                    alert("请上传xls、xlsx格式文件!");
                    return;
                }
            }
        });

        //更新学生信息
        $("#updateSubmitButton").click(function(){
            var courseid = $("#updateCourseId").val();
            var coursename = $("#updateCourseName").val();
            var coursescore = $("#updateCourseScore").val();
            var coursetime = $("#updateCourseTime").val();
            var coursetype = $("#updateCourseType option:selected ").val();
            var courseteacher = $("#updateCourseTeacher option:selected").val();

            var coursestartweek = $("#updateCourseStartWeek").val();
            var courseenddweek = $("#updateCourseEndWeek").val();
            var coursestate = $("#updateCourseState option:selected").val();

            if(coursename.length == 0){
                alert("请正确填写课程名称!");
                return;
            }

            if(coursestartweek.length == 0){
                alert("请填写起始周信息");
                return;
            }

            if(courseendweek.length == 0){
                alert("请填写终止周信息");
                return;
            }


            if(coursename.length == 0){
                alert("请正确填写课程名称!");
                return;
            }


            coursescore = parseFloat(coursescore)
            coursetime = parseInt(coursetime)
            coursestartweek = parseInt(coursestartweek);
            courseenddweek = parseInt(courseenddweek);

            if(coursestartweek <= 0){
                alert("起始周错误");
                return;
            }

            if(courseendweek <= 0){
                alert("终止周错误");
                return;
            }

            $.ajax({
                url:"/studentManageSystem/updateCourseInfo",
                type:"post",
                data:{courseid:courseid,coursename:coursename,coursescore:coursescore,coursetime:coursetime,coursetype:coursetype,courseteacher:courseteacher,coursestartweek:coursestartweek,courseendweek:courseenddweek,coursestate:coursestate},
                success:function(data){
                    if(data=="OK"){
                        alert("修改成功");
                        $table.bootstrapTable('refresh');
                        $("#updateModal").modal("hide");
                    }else{
                        alert("修改失败");
                    }
                }
            })
        });


        function init(){
            $("#coursetype").empty();
            $.ajax({
                url:"/util/getCourseTypes",
                type:"post",
                success:function(data){
                    for(var i = 0;i<data.length;i++){
                        $("#coursetype").append("<option value="+data[i]['coursetypename']+">"+data[i]['coursetypename']+"</option>")
                    }
                }
            });


            $("#updateCourseType").empty();
            $.ajax({
                url:"/util/getCourseTypes",
                type:"post",
                success:function(data){
                    for(var i = 0;i<data.length;i++){
                        $("#updateCourseType").append("<option value="+data[i]['coursetypename']+">"+data[i]['coursetypename']+"</option>")
                    }
                }
            });

            $("#courseTeacher").empty();
            $.ajax({
                url:"/util/getTeacherInfos",
                type:"post",
                success:function(data){
                    for(var i = 0;i<data.length;i++){
                        $("#courseTeacher").append("<option value="+data[i]['id']+">"+data[i]['name']+"</option>")
                    }
                }
            });

            $("#updateCourseTeacher").empty();
            $.ajax({
                url:"/util/getTeacherInfos",
                type:"post",
                success:function(data){
                    for(var i = 0;i<data.length;i++){
                        $("#updateCourseTeacher").append("<option value="+data[i]['id']+">"+data[i]['name']+"</option>")
                    }
                }
            });

        }

        // bootstrap table初始化
        $table.bootstrapTable({
            url: '/studentManageSystem/getCourseInfos',
            height: getHeight(),
            striped: true,
            search: true,
            showRefresh: true,
            showColumns: true,
            minimumCountColumns: 2,
            clickToSelect: true,
            detailView: true,
            pagination: true,
            paginationLoop: false,
            sidePagination: 'server',
            silentSort: false,
            sortOrder: "desc",
            smartDisplay: false,
            escape: true,
            searchOnEnterKey: true,
            idField: 'id',
            maintainSelected: true,
            toolbar: '#toolbar',
            columns: [
                {field: 'ck', checkbox: true},
                {field: 'courseid', title: '课程编号', align: 'center'},
                {field: 'coursename', title: '课程名称',align: 'center'},
                {field: 'coursescore', title: '绩点',align: 'center'},
                {field:'courseteacher',title:'教师编号',align:'center'},
                {field:'teacherName',title:'教师',align:'center'},
                {field: 'coursetime', title: '课时',align: 'center'},
                {field: 'coursetype', title: '课程类型',align: 'center'},
                {field:'coursestartweek',title:'起始周',align:'center'},
                {field:'courseendweek',title:'结束周',align:'center'},
                {field:'coursestate',title:'状态',align:'center'},
                {field: 'action', title: '操作', align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: false}
            ]
        });
    });

    // 格式化操作按钮
    function actionFormatter(value, row, index) {
        return [
            '<a class="update" href="javascript:;" onclick="updateAction()" data-toggle="tooltip" title="编辑"><i class="glyphicon glyphicon-edit"></i></a>　',
            '<a class="delete" href="javascript:;" onclick="deleteAction()" data-toggle="tooltip" title="删除"><i class="glyphicon glyphicon-remove"></i></a>'
        ].join('');
    }
    // 格式化时间
    function timeFormatter(value , row, index) {
        return new Date(value).toLocaleString().split(' ')[0];
    }

    function getHeight() {
        return $(window).height() - 20;
    }

    // 删除
    var deleteDialog;
    function deleteAction() {
        var rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            $.confirm({
                title: false,
                content: '请至少选择一条记录！',
                autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        } else {
            deleteDialog = $.confirm({
                type: 'red',
                animationSpeed: 300,
                title: false,
                content: '确认删除该课程信息吗？',
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'waves-effect waves-button',
                        action: function () {
                            var ids = new Array();
                            for (var i in rows) {
                                ids.push(rows[i].courseid);
                            }
                            $.ajax({
                                type: 'get',
                                url: '/studentManageSystem/deleteCourseinfo/'+ids.join("-"),
                                success: function(result) {
                                    if(result == "OK"){
                                        deleteDialog.close();
                                        $table.bootstrapTable('refresh');
                                        alert("删除成功");
                                    }else{
                                        alert("删除失败");
                                    }
                                },
                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                    $.confirm({
                                        theme: 'dark',
                                        animation: 'rotateX',
                                        closeAnimation: 'rotateX',
                                        title: false,
                                        content: textStatus,
                                        buttons: {
                                            confirm: {
                                                text: '确认',
                                                btnClass: 'waves-effect waves-button waves-light'
                                            }
                                        }
                                    });
                                }
                            });
                        }
                    },
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        }
    }

    //创建课程信息
    function createAction(){
        $("#createModal").modal("show");
    }

    //上传课程信息
    function uploadAction(){
        $("#uploadModal").modal('show');
    }

    //编辑课程信息
    function updateAction() {
        var rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            $.confirm({
                title: false,
                content: '请至少选择一条记录！',
                autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        } else {
            //赋初值
            var obj = rows[0];
            var courseid = obj["courseid"];
            var coursename = obj["coursename"];
            var coursescore = obj["coursescore"];
            var coursetime = obj["coursetime"];
            var coursetype = obj["coursetype"];
            var updatecourseteacher = obj["courseteacher"];
            var updatecoursestartweek = obj["coursestartweek"];
            var updatecourseendweek = obj["courseendweek"];
            var updatecoursestate = obj["coursestate"];

            //设置修改信息的值
            $("#updateCourseId").val(courseid);
            $("#updateCourseName").val(coursename);
            $("#updateCourseScore").val(coursescore);
            $("#updateCourseTime").val(coursetime);
            $("#updateCourseType option[value='"+coursetype+"']").attr("selected","selected");
            $("#updateCourseTeacher option[value='"+updatecourseteacher+"']").attr("selected","selected");
            $("#updateCourseStartWeek").val(updatecoursestartweek);
            $("#updateCourseEndWeek").val(updatecourseendweek);
            $("#updateCourseState option[value='"+updatecoursestate+"']").attr("selected","selected");
            $("#updateModal").modal('show');
        }
    }

</script>

<!-- 添加课程信息模态框（Modal） -->
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="createModalTitle">
                    添加课程信息
                </h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="coursename">课程名称</label>
                        <input type="text" class="form-control" id="coursename" placeholder="courseName">
                    </div>
                    <div class="form-group">
                        <label for="coursescore">课程绩点</label>
                        <input type="number" class="form-control" min="0" max="5" id="coursescore" placeholder="courseScore"/>
                    </div>

                    <div class="form-group">
                        <label for="coursetime">学时</label>
                        <input type="number" class="form-control" min="0" max="64" id="coursetime" placeholder="courseTime"/>
                    </div>

                    <div class="form-group">
                        <label for="coursetype">课程类型</label>
                        <select class="form-control" id="coursetype" name="coursetype"></select>
                    </div>

                    <div class="form-group">
                        <label for="courseTeacher">教师</label>
                        <select class="form-control" id="courseTeacher" name="courseTeacher"></select>
                    </div>

                    <div class="form-group">
                        <label for="coursestartweek">起始周</label>
                        <input type="number" class="form-control" min="1" id="coursestartweek" name="coursestartweek" placeholder="coursestartweek"/>
                    </div>

                    <div class="form-group">
                        <label for="courseendweek">终止周</label>
                        <input type="number" class="form-control" min="1" id="courseendweek" name="courseendweek" placeholder="courseendweek"/>
                    </div>

                    <div class="form-group">
                        <label for="coursestate">状态</label>
                        <select class="form-control" id="coursestate" name="coursestate" placeholder="coursestate">
                            <option value="结课">结课</option>
                            <option value="未结课">未结课</option>
                            <option value="未开课">未开课</option>
                        </select>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="submitbutton">
                    添加
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>


<!-- 修改课程信息模态框（Modal） -->
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="updateModalTitle">
                    编辑课程信息
                </h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="updateCourseId">课程编号</label>
                        <input type="text" class="form-control" id="updateCourseId" name="updateCourseId" placeholder="CourseId" readonly="readonly"/>
                    </div>
                    <div class="form-group">
                        <label for="updateCourseName">课程名称</label>
                        <input type="text" class="form-control" id="updateCourseName" name="updateCourseName" placeholder="CourseName"/>
                    </div>
                    <div class="form-group">
                        <label for="updateCourseScore">课程绩点</label>
                        <input class="form-control" id="updateCourseScore" name="updateCourseScore" placeholder="CourseScore"/>
                    </div>
                    <div class="form-group">
                        <label for="updateCourseTime">课程学时</label>
                        <input class="form-control" id="updateCourseTime" name="updateCourseTime" placeholder="CourseTime"/>
                    </div>
                    <div class="form-group">
                        <label for="updateCourseType">课程类型</label>
                        <select class="form-control" id="updateCourseType" name="updateCourseType" placeholder="CourseType"></select>
                    </div>

                    <div class="form-group">
                        <label for="updateCourseTeacher">教师</label>
                        <select class="form-control" id="updateCourseTeacher" name="updateCourseTeacher" placeholder="CourseTeacher"></select>
                    </div>

                    <div class="form-group">
                        <label for="updateCourseStartWeek">起始周</label>
                        <input type="number" class="form-control" min="1" id="updateCourseStartWeek" name="updateCourseStartWeek" placeholder="coursestartweek"/>
                    </div>

                    <div class="form-group">
                        <label for="updateCourseEndWeek">终止周</label>
                        <input type="number" class="form-control" min="1" id="updateCourseEndWeek" name="updateCourseEndWeek" placeholder="courseendweek"/>
                    </div>

                    <div class="form-group">
                        <label for="updateCourseState">状态</label>
                        <select class="form-control" id="updateCourseState" name="updateCourseState" placeholder="coursestate">
                            <option value="结课">结课</option>
                            <option value="未结课">未结课</option>
                            <option value="未开课">未开课</option>
                        </select>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="updateSubmitButton">
                    确认修改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<!-- 上传课程信息模态框 (Modal）-->
<div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="uploadModalTitle">
                    导入教师信息
                </h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="file">上传文件</label>
                        <input type="file" class="form-control" id="file" name="file" type="tel"/>
                    </div>
                </form>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="uploadbutton">
                    添加
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

</body>
</html>